import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export default function checkPermission(value) {
  const roles = store.getters && store.getters.roles
  // 超级管理员拥有所有的按钮权限
  if (roles.includes('SUPER_ADMIN')) {
    return true
  }
  const perms = store.getters && store.getters.perms
  if (value && value instanceof Array && value.length > 0) {
    const requiredPerms = value
    const hasPermission = perms.some(perm => {
      return requiredPerms.includes(perm)
    })
    return hasPermission
  } else {
    throw new Error(
      `need perms! Like v-has-permission="['system:user:add','system:user:edit']"`
    )
  }
}
